package cn.life.common.utils.PDF.apply;

import cn.life.business.apply.entity.po.ApplyPo;
import cn.life.business.audit.entity.po.AuditPo;
import cn.life.common.utils.PDF.PDFBuilder;
import cn.life.common.utils.PDF.PdfElement;
import cn.life.common.utils.PDF.PdfUtil;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Description:
 * @Author 李佳明
 * @Date 2018-10-09 16:33
 */
public class AuditTemplate {


    public boolean AuditTemplate(String prefixUrl,String pdfName,AuditPo auditPo,String imgPrefixUrl) {
        /**
         * @author  By SuperChen:
         * @version 创建时间：2016年11月21日 下午2:29:51
         * PDFReportTODO
         */
        Document document = new Document();// 建立一个Document对象

        try {
            /**
             *  字体
             */
            BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/simhei.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
            // 标题字体
            Font FontChinese18 = new Font(bfChinese, 18,com.itextpdf.text.Font.BOLD);
            Font FontChinese99 = new Font(bfChinese, 99,com.itextpdf.text.Font.BOLD);
            // 字体格式
            Font fontChinese11Normal = new Font(bfChinese,11,Font.NORMAL);
            Font fontChinese13Normal = new Font(bfChinese,13,Font.NORMAL);
            Font fontChinese16Normal = new Font(bfChinese,16,Font.NORMAL);

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
            String nowDate = dateFormat.format(auditPo.getOprDate());

            String fileUrl = prefixUrl + pdfName;
            File file = new File(fileUrl);
            file.createNewFile();
            Rectangle pageSize = new Rectangle(PageSize.A4);
            document.setPageSize(pageSize);
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
            PDFBuilder builder = new PDFBuilder();
            writer.setPageEvent(builder);
            document.open();

            int firstX = 0;
            int firstY = 0;

            //导出申请单中盖章图片
            Image image1 = Image.getInstance(auditPo.getCertifiedImgUrl());  //写入图片
            image1.scaleAbsolute(595, 842);// 控制图片大小 (宽,高)
            image1.setAbsolutePosition(firstX, firstY);// 控制图片位置
            document.add(image1);

            Paragraph blankRow1 = new Paragraph(18f, " ", FontChinese18);
            document.add(blankRow1);
            Paragraph blankRow2 = new Paragraph(18f, " ", FontChinese18);
            document.add(blankRow2);
            Paragraph blankRow3 = new Paragraph(18f, " ", FontChinese18);
            document.add(blankRow3);
            Paragraph blankRow4 = new Paragraph(18f, " ", FontChinese18);
            document.add(blankRow4);
            Paragraph blankRow5 = new Paragraph(18f, " ", FontChinese18);
            document.add(blankRow5);
            Paragraph blankRow6 = new Paragraph(18f, " ", FontChinese18);
            document.add(blankRow6);
            Paragraph blankRow7 = new Paragraph(18f, " ", FontChinese18);
            document.add(blankRow7);
            Paragraph blankRow8 = new Paragraph(18f, " ", FontChinese18);
            document.add(blankRow8);
            Paragraph blankRow9 = new Paragraph(18f, " ", FontChinese18);
            document.add(blankRow9);
            Paragraph blankRow11 = new Paragraph(18f, " ", FontChinese18);
            document.add(blankRow11);
            Paragraph blankRow12 = new Paragraph(18f, " ", FontChinese18);
            document.add(blankRow12);
            Paragraph blankRow13 = new Paragraph(18f, " ", FontChinese18);
            document.add(blankRow13);
            Paragraph blankRow14 = new Paragraph(18f, " ", FontChinese18);
            document.add(blankRow14);
            Paragraph blankRow15 = new Paragraph(18f, " ", FontChinese18);
            document.add(blankRow15);
            Paragraph blankRow16 = new Paragraph(18f, " ", FontChinese18);
            document.add(blankRow16);
            Paragraph blankRow17 = new Paragraph(18f, " ", FontChinese18);
            document.add(blankRow17);
            Paragraph blankRow18 = new Paragraph(18f, " ", FontChinese18);
            document.add(blankRow18);
            Paragraph blankRow19 = new Paragraph(18f, " ", FontChinese18);
            document.add(blankRow19);
            Paragraph blankRow21 = new Paragraph(18f, " ", FontChinese18);
            document.add(blankRow21);
            Paragraph blankRow22 = new Paragraph(18f, " ", FontChinese18);
            document.add(blankRow22);
            Paragraph blankRow23 = new Paragraph(18f, " ", FontChinese18);
            document.add(blankRow23);
            Paragraph blankRow25 = new Paragraph(18f, " ", FontChinese18);
            document.add(blankRow25);
            Paragraph blankRow26 = new Paragraph(16f, " ", fontChinese16Normal);
            document.add(blankRow26);

            /**
             * 赋值
             */
            Paragraph srjName = new Paragraph("      朝阳区市容局                                   "+nowDate+"",fontChinese11Normal);
            srjName.setIndentationLeft(100f);
            srjName.setSpacingBefore(5f);
            srjName.setSpacingAfter(16f);
            document.add(srjName);

            Paragraph auditDesc = new Paragraph(auditPo.getAuditDesc(),fontChinese11Normal);
            auditDesc.setIndentationLeft(105f);
            document.add(auditDesc);

            /**
             * 盖章
             */
            String imageUrl = imgPrefixUrl + "srjSeal.jpg";
            Image image2 = Image.getInstance(imageUrl);  //写入图片
            image2.scaleAbsolute(150, 150);// 控制图片大小 (宽,高)
            image2.setAbsolutePosition(300,120);// 控制图片位置
            document.add(image2);

            /**
             *  年月日
             */
            String now = dateFormat.format(new Date());
            Paragraph endDate = new Paragraph("                                                         " + now,fontChinese13Normal);
            Paragraph blankRow33 = new Paragraph(99f, " ", FontChinese99);
            document.add(blankRow33);
            document.add(blankRow25);
            document.add(blankRow25);
            document.add(blankRow25);
            document.add(blankRow25);
            document.add(blankRow25);
            document.add(blankRow25);
            document.add(endDate);

            document.close();
            return true;
        } catch (Exception e) {
            document.close();
            e.printStackTrace();
            return false;
        }
    }
}
